package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.query.ArticleTypeQuery;
import com.xxx.blog.service.IArticleTypeService;

import com.xxx.blog.dto.ArticleTypeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ArticleType")
@Api(tags = "博客文章类型管理类接口")
public class ArticleTypeController {
    @Autowired
    private IArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<ArticleType>> finfAll(){
        List<ArticleType> list = articleTypeService.findAll();
        return R.ok(list);
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "ID查询单个")
    public R<ArticleType> findOne(@PathVariable Long id) {
        ArticleType ArticleType = articleTypeService.findOne(id);
        return R.ok(ArticleType);
    }
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageInfo<ArticleType> findByPage(@RequestBody ArticleTypeQuery articleTypeQuery) {
        List<ArticleType> byPage = articleTypeService.findByPage(articleTypeQuery);
        PageInfo<ArticleType> pageInfo = new PageInfo<>(byPage);
        return pageInfo;
    }

    @PutMapping
    @ApiOperation(value = "增加或者修改")
    public R<Void> updateOrInsert(@RequestBody ArticleType articleType){
        try {
            if (articleType.getId()!=null){
                articleTypeService.updateById(articleType);
            }else {
                articleTypeService.insert(articleType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "单个删除")
    public R<Void> delete(@PathVariable Long id){
        articleTypeService.deleteById(id);
        return R.ok();
    }


    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> bathArticleTypes(@RequestBody Long[] ids) {
        try {
            articleTypeService.bathArticleTypes(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/getTypeTree/{id}")
    @ApiOperation(value = "级联数据")
    public R<List<ArticleType>> getTypeTree(@PathVariable Long id){
        List<ArticleType> list = articleTypeService.getTypeTree(id);
        return R.ok(list);
    }

    @GetMapping("/articleTypeSta")
    @ApiOperation(value = "分类统计博客文章")
    public List<ArticleTypeDTO> articleTypeSta(){
        return articleTypeService.getArticleTypeSta();
    }
}
